<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="AuthCode.userAdd">新增用户</a-button>
      </template>
      <template #roles="{ record }">
        {{ record['roles'].join(',') }}
      </template>
      <template #action="{ record }">
        <div class="flex justify-around">
          <Button type="primary" @click="handleEdit(record)" v-auth="AuthCode.userUpdate">
            <Icon icon="clarity:note-edit-line" />
            编辑
          </Button>
          <Button
            type="primary"
            @click="handleAssignPermission(record)"
            v-auth="AuthCode.userAssignRole"
          >
            <Icon icon="clarity:note-edit-line" />
            分配角色
          </Button>
          <div v-auth="AuthCode.userDestroy">
            <PopConfirmButton
              :title="`确定删除-${record.username}?`"
              @confirm="handleDelete(record)"
              ok-text="确定"
              danger
            >
              <Icon icon="ant-design:delete-outlined" /> 删除
            </PopConfirmButton>
          </div>
        </div>
      </template>
    </BasicTable>
    <UserModal @register="registerModal" @success="handleSuccess" />
    <AssignRoleModal @register="registerAssignRoleModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { useTable, BasicTable } from '/@/components/Table';
  import { AuthCode } from '/@/settings/authSetting';
  import { PopConfirmButton, Button } from '/@/components/Button';
  import Icon from '/@/components/Icon/index';
  import { useModal } from '/@/components/Modal';
  import UserModal from './UserModal.vue';
  import AssignRoleModal from './AssignRoleModal.vue';

  import { columns, searchFormSchema } from '/@/views/system/user/data';

  import { getUserDetail, getUserList } from '/@/api/sys/user';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerAssignRoleModal, { openModal: openAssignRoleModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '用户列表',
    columns,
    api: getUserList,
    pagination: {
      pageSize: 4,
    },
    useSearchForm: true,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    actionColumn: {
      title: '操作',
      width: 180,
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
    inset: true,
    striped: false,
    canResize: false,
    bordered: false,
    showIndexColumn: false,
    showTableSetting: true,
  });

  function handleAssignPermission(record) {
    console.log(record);
    openAssignRoleModal(true, {
      record,
    });
  }

  function handleDelete(record) {
    console.log(record);
    createMessage.warning('暂不支持此功能');
  }

  async function handleEdit(record) {
    let data = await getUserDetail(record.id);
    openModal(true, {
      isUpdate: true,
      record: data,
    });
  }

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleSuccess() {
    reload();
  }
</script>

<style scoped lang="less"></style>
